import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import { analysisApi } from '../api/analysisApi'

export const useAnalysisStore = defineStore('analysis', {
  state: () => ({
    systemStatistics: null,
    cityStatistics: {},
    districtAnalysis: {},
    loading: false
  }),
  
  actions: {
    // 获取系统统计数据
    async fetchSystemStatistics() {
      try {
        this.loading = true
        this.systemStatistics = await analysisApi.getSystemStatistics()
        return this.systemStatistics
      } catch (error) {
        console.error('获取系统统计数据失败:', error)
        throw error
      } finally {
        this.loading = false
      }
    },
    
    // 获取热门城市统计数据
    async fetchHotCityStatistics() {
      try {
        this.loading = true
        return await analysisApi.getHotCityStatistics()
      } catch (error) {
        console.error('获取热门城市统计数据失败:', error)
        throw error
      } finally {
        this.loading = false
      }
    },
    
    // 获取城市统计数据
    async fetchCityStatistics(city) {
      try {
        this.loading = true
        if (this.cityStatistics[city]) {
          return this.cityStatistics[city]
        }
        
        this.cityStatistics[city] = await analysisApi.getCityStatistics(city)
        return this.cityStatistics[city]
      } catch (error) {
        console.error('获取城市统计数据失败:', error)
        throw error
      } finally {
        this.loading = false
      }
    },
    
    // 获取价格分布数据
    async fetchPriceDistribution(city) {
      try {
        this.loading = true
        return await analysisApi.getPriceDistribution(city)
      } catch (error) {
        console.error('获取价格分布数据失败:', error)
        throw error
      } finally {
        this.loading = false
      }
    },
    
    // 获取面积分布数据
    async fetchAreaDistribution(city) {
      try {
        this.loading = true
        return await analysisApi.getAreaDistribution(city)
      } catch (error) {
        console.error('获取面积分布数据失败:', error)
        throw error
      } finally {
        this.loading = false
      }
    },
    
    // 获取户型分布数据
    async fetchLayoutDistribution(city) {
      try {
        this.loading = true
        return await analysisApi.getLayoutDistribution(city)
      } catch (error) {
        console.error('获取户型分布数据失败:', error)
        throw error
      } finally {
        this.loading = false
      }
    },
    
    // 获取装修分布数据
    async fetchDecorationDistribution(city) {
      try {
        this.loading = true
        return await analysisApi.getDecorationDistribution(city)
      } catch (error) {
        console.error('获取装修分布数据失败:', error)
        throw error
      } finally {
        this.loading = false
      }
    },
    
    // 获取价格-面积散点图数据
    async fetchPriceAreaScatter(city) {
      try {
        this.loading = true
        return await analysisApi.getPriceAreaScatter(city)
      } catch (error) {
        console.error('获取价格-面积散点图数据失败:', error)
        throw error
      } finally {
        this.loading = false
      }
    },
    
    // 获取价格趋势数据
    async fetchPriceTrends(city) {
      try {
        this.loading = true
        return await analysisApi.getPriceTrends(city)
      } catch (error) {
        console.error('获取价格趋势数据失败:', error)
        throw error
      } finally {
        this.loading = false
      }
    },
    
    // 获取区域分析数据
    async fetchDistrictAnalysis(city) {
      try {
        this.loading = true
        if (this.districtAnalysis[city]) {
          return this.districtAnalysis[city]
        }
        
        this.districtAnalysis[city] = await analysisApi.getDistrictAnalysis(city)
        return this.districtAnalysis[city]
      } catch (error) {
        console.error('获取区域分析数据失败:', error)
        throw error
      } finally {
        this.loading = false
      }
    },
    
    // 获取区域详细数据
    async fetchDistrictDetail(city, district) {
      try {
        this.loading = true
        return await analysisApi.getDistrictDetail(city, district)
      } catch (error) {
        console.error('获取区域详细数据失败:', error)
        throw error
      } finally {
        this.loading = false
      }
    },
    
    // 获取城市对比数据
    async fetchCityComparison(cities) {
      try {
        this.loading = true
        return await analysisApi.getCityComparison(cities)
      } catch (error) {
        console.error('获取城市对比数据失败:', error)
        throw error
      } finally {
        this.loading = false
      }
    },
    
    // 获取房源价格分析
    async getHousePriceAnalysis(id) {
      try {
        this.loading = true
        return await analysisApi.getHousePriceAnalysis(id)
      } catch (error) {
        console.error('获取房源价格分析失败:', error)
        throw error
      } finally {
        this.loading = false
      }
    },
    
    // 清除缓存
    async clearCache(prefix) {
      try {
        return await analysisApi.clearCache(prefix)
      } catch (error) {
        console.error('清除缓存失败:', error)
        throw error
      }
    },
    
    // 清除所有分析缓存
    async clearAllAnalysisCache() {
      try {
        return await analysisApi.clearAllAnalysisCache()
      } catch (error) {
        console.error('清除所有分析缓存失败:', error)
        throw error
      }
    },
    
    // 获取缓存统计信息
    async getCacheStats() {
      try {
        return await analysisApi.getCacheStats()
      } catch (error) {
        console.error('获取缓存统计信息失败:', error)
        throw error
      }
    }
  }
}) 